﻿// See https://aka.ms/new-console-template for more information

using Hsp.Media.App.Utility;
using Hsp.Media.Model.Models;
using Microsoft.Extensions.Configuration;
//using System.Text.Encoding.CodePages;
using SqlSugar;
using System.Text;

Console.WriteLine("Hello, SqlSugar!");
Console.WriteLine("*********************************************");
Console.WriteLine("");


Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);


IConfigurationRoot _configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build();

var sqlConnectionString = ConfigurationExtensions.GetConnectionString(_configuration, "MediaConnection");

//Creaate database object
SqlSugarClient db = new(new ConnectionConfig() { ConfigId = "0", DbType = DbType.SqlServer, ConnectionString = sqlConnectionString, IsAutoCloseConnection = true });

//用来打印Sql方便你调式    
db.Aop.OnLogExecuting = (sql, pars) =>
{
    Console.WriteLine(sql + "\r\n" +
    db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
    Console.WriteLine();
};



{
    //var srt = @"E:\电影\追光寻影（zgxybbs.fdns.uk）沙丘.Dune.2021.1080p.WEBRip.DD5.1.x264-纯净版\[zmk.pw]Dune.2021.1080p.WEBRip.DD5.1.x264-SHITBOX.srt";
    //int fileId = 1;
    //SrtHelper.GetSrt(fileId, db, srt);
}

{
    //var srt = @"E:\电影\追光寻影（zgxybbs.fdns.uk）沙丘.Dune.2021.1080p.WEBRip.DD5.1.x264-纯净版\[zmk.pw]Dune.2021.HMAX.WEB-DL.1080p.x265.DDP5.1.Atmos-PTHweb.chs&amp;eng.srt";
    //int fileId = 2;
    //SrtHelper.GetSrt(fileId, db, srt);
}

{
    //    UPDATE a
    //SET StartTime = b.StartTime, EndTime = b.EndTime, Chinese = b.Chinese
    //FROM[dbo].[Media_Subtitle_Content] a
    //INNER JOIN[dbo].[Media_Subtitle_Content] b ON b.FileId = 1 AND b.Seq = a.Seq
    //WHERE(a.FileId = 2);
}

{
    //int fileId = 2;
    //var file = db.Queryable<MediaSubtitleFile>().First(f => f.Id == fileId);

    //List<MediaSubtitleContent> contents = db.Queryable<MediaSubtitleContent>().Where(c=>c.FileId == fileId).ToList();
    //foreach (var content in contents)
    //{
    //    Console.WriteLine(content.Seq);
    //    Console.WriteLine(SrtHelper.GetTimeString(content));
    //    if (file?.TypeId == 0) Console.WriteLine(content.Chinese);
    //    if (file?.TypeId == 1) Console.WriteLine(content.English);
    //    if (file?.TypeId == 2)
    //    {
    //        if (!string.IsNullOrEmpty(content.Chinese)) Console.WriteLine(content.Chinese);
    //        if(!string.IsNullOrEmpty(content.English))Console.WriteLine(content.English);
    //    }

    //    Console.WriteLine("");
    //}
}

{
    // SqlSugar 事务测试

    try
    {
        db.BeginTran();

        MediaSubtitleFile file = new();
        file.Chinese = "沙丘";
        file.English = "Dune";
        file.FileName = "追光寻影（zgxybbs.fdns.uk）沙丘.Dune.2021.1080p.WEBRip.DD5.1.x264-纯净版.MKV";
        file.FileEncoding = "GBK";
        file.TypeId = 0;
        file.Remark = "";
        file.CreateTime = DateTime.Now;

        var id = db.Insertable(file).ExecuteReturnIdentity();
        file.Id = id;
        Console.WriteLine(id);

        //throw new Exception("添加子项出错");

        var srt = @"E:\电影\追光寻影（zgxybbs.fdns.uk）沙丘.Dune.2021.1080p.WEBRip.DD5.1.x264-纯净版\[zmk.pw]Dune.2021.1080p.WEBRip.DD5.1.x264-SHITBOX.srt";
        int fileId = file.Id;
        Console.WriteLine(id);
        //SrtHelper.GetSrt(fileId, db, srt);
        //var file = db.Queryable<MediaSubtitleFile>().First(f => f.Id == fileId);
        //if (file == null) return;
        Encoding encoding = Encoding.GetEncoding(file?.FileEncoding ?? "GBK");

        List<MediaSubtitleContent> contents = SrtHelper.ParseSrt(file?.Id, srt, file?.TypeId, encoding);
        foreach (var content in contents)
        {
            Console.WriteLine(content.Seq);
            Console.WriteLine(SrtHelper.GetTimeString(content));
            if (file?.TypeId == 0) Console.WriteLine(content.Chinese);
            if (file?.TypeId == 1) Console.WriteLine(content.English);
            if (file?.TypeId == 2)
            {
                Console.WriteLine(content.Chinese);
                Console.WriteLine(content.English);
            }

            Console.WriteLine("");
        }

        db.Deleteable<MediaSubtitleContent>().Where(c => c.FileId == fileId).ExecuteCommand();
        db.Insertable(contents).ExecuteCommand();

        db.CommitTran();
    }
    catch (Exception ex)
    {
        db.RollbackTran();

        Console.WriteLine(ex.Message);
        //throw;
    }










}



Console.WriteLine("");
Console.WriteLine("*********************************************");
Console.WriteLine("Bye, SqlSugar!");
Console.ReadKey();

